/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.item.etcitem.action;

import com.lineage.server.codes.ActionCodes;
import com.lineage.server.datatables.FurnitureSpawnTable;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1PcInventory;
import com.lineage.server.model.L1World;
import com.lineage.server.model.Instance.L1FurnitureInstance;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.item.ItemExecutor;
import com.lineage.server.packetserver.S_AttackPacket;

/**
 * Class <code>WandRemoveFurniture</code> 移除家具魔杖 - 41401.
 * 
 * @author jrwz
 * @version 2012-6-19下午14:52:01
 * @see com.lineage.server.model.item.etcitem.action
 * @since JDK1.7
 */
public final class WandRemoveFurniture extends ItemExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static ItemExecutor get() {
        return new WandRemoveFurniture();
    }

    /** 移除家具魔杖 - 41401. */
    private WandRemoveFurniture() {
    }

    @Override
    public void execute(final L1PcInstance pc, final L1ItemInstance useItem,
            final int readC1, final int readC2, final int targetId,
            final int targetX, final int targetY) {

        final int spellscObjid = targetId;
        this.useFurnitureRemovalWand(pc, spellscObjid, useItem);
    }

    //
    /**
     * 移除家具.
     * 
     * @param pc
     *            - 对象
     * @param targetId
     *            - 目标
     * @param item
     *            - 道具
     */
    private void useFurnitureRemovalWand(final L1PcInstance pc,
            final int targetId, final L1ItemInstance item) {
        final S_AttackPacket attackPacket = new S_AttackPacket(pc, 0,
                ActionCodes.ACTION_WAND);
        pc.sendPackets(attackPacket);
        pc.broadcastPacket(attackPacket);
        final int chargeCount = item.getChargeCount();
        if (chargeCount <= 0) {
            return;
        }

        final L1Object target = L1World.getInstance().findObject(targetId);
        if ((target != null) && (target instanceof L1FurnitureInstance)) {
            final L1FurnitureInstance furniture = (L1FurnitureInstance) target;
            furniture.deleteMe();
            FurnitureSpawnTable.getInstance().deleteFurniture(furniture);
            item.setChargeCount(item.getChargeCount() - 1);
            pc.getInventory().updateItem(item, L1PcInventory.COL_CHARGE_COUNT);
        }
    }
}
